package org.jeecg.modules.wlhy.gas.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.third.nucarf.GasService;
import org.jeecg.modules.wlhy.gas.entity.HyShippingNoteGas;
import org.jeecg.modules.wlhy.gas.service.IHyShippingNoteGasService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: hy_shipping_note_gas
 * @Author: jeecg-boot
 * @Date:   2021-08-14
 * @Version: V1.0
 */
@Api(tags="hy_shipping_note_gas")
@RestController
@RequestMapping("/gas/hyShippingNoteGas")
@Slf4j
public class HyShippingNoteGasController extends JeecgController<HyShippingNoteGas, IHyShippingNoteGasService> {

	@Autowired
	private IHyShippingNoteGasService hyShippingNoteGasService;

	@Autowired
	private IHyShippingNoteService hyShippingNoteService;

	@Autowired
	private GasService gasService;


	@ApiOperation(value = "hy_shipping_note_gas-分页列表查询", notes = "hy_shipping_note_gas-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyShippingNoteGas hyShippingNoteGas,
								   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyShippingNoteGas> queryWrapper = QueryGenerator.initQueryWrapper(hyShippingNoteGas, req.getParameterMap());
		Page<HyShippingNoteGas> page = new Page<HyShippingNoteGas>(pageNo, pageSize);
		IPage<HyShippingNoteGas> pageList = hyShippingNoteGasService.page(page, queryWrapper);

		List<HyShippingNoteGas> hyShippingNoteGass = pageList.getRecords();
		if (CollectionUtils.isNotEmpty(hyShippingNoteGass)) {
			List<String> shippingIds = hyShippingNoteGass.stream().map(
					item -> item.getShippingId()
			).distinct().filter(Objects::nonNull).collect(Collectors.toList());

			List<HyShippingNote> hyShippingNotes =  hyShippingNoteService.listByIds(shippingIds);
			Map<String, String> hyShippingNotesMaps = hyShippingNotes.stream().collect(
					Collectors.toMap(HyShippingNote::getId, HyShippingNote::getShippingPayStatus, (key1, key2) -> key2)
			);
			for(HyShippingNoteGas shippingNoteGas : hyShippingNoteGass){
				shippingNoteGas.setShippingPayStatus( hyShippingNotesMaps.get(shippingNoteGas.getShippingId()) );
			}
		}

		return Result.OK(pageList);
	}

	@GetMapping(value = "/getGasAccInfo")
	public Result<?> getGasAccInfo() {
		return gasService.getGasAccInfo();
	}

	@GetMapping(value = "/getHgGasAccInfo")
	public Result<?> getHgGasAccInfo() {
		return gasService.getHgGasAccInfo();
	}

//	/**
//	 *  编辑
//	 *
//	 * @param hyShippingNoteGas
//	 * @return
//	 */
//	@AutoLog(value = "hy_shipping_note_gas-编辑")
//	@ApiOperation(value="hy_shipping_note_gas-编辑", notes="hy_shipping_note_gas-编辑")
//	@PutMapping(value = "/edit")
//	public Result<?> edit(@RequestBody HyShippingNoteGas hyShippingNoteGas) {
//		hyShippingNoteGasService.updateById(hyShippingNoteGas);
//		return Result.OK("编辑成功!");
//	}


	@AutoLog(value = "油气充值失败，重新充值")
	@ApiOperation(value = "油气充值失败，重新充值", notes = "油气充值失败，重新充值")
	@PutMapping(value = "/recharge")
	public Result<?> recharge(@RequestParam(name = "id", required = true) String id) {
		return gasService.rechargeFail(id);
	}

	@AutoLog(value = "油气充值回收，重新充值")
	@ApiOperation(value = "油气充值回收，重新充值", notes = "油气充值回收，重新充值")
	@PutMapping(value = "/rechargeBack")
	public Result<?> rechargeBack(@RequestBody JSONObject jsonObject) {
		String id = jsonObject.getString("id");
		String money = jsonObject.getString("money");
		String vehicleEnergyType = jsonObject.getString("vehicleEnergyType");

		if(StringUtils.isBlank(id)){
			return Result.error("重新充值信息不能为空!");
		}

		if(StringUtils.isBlank(money)){
			return Result.error("充值金额不能为空!");
		}
		BigDecimal moneyBigDecimal = new BigDecimal(money);
		if(moneyBigDecimal.compareTo(BigDecimal.ZERO) == 0){
			return Result.error("充值金额不能为空!");
		}

		if(StringUtils.isBlank(vehicleEnergyType)){
			return Result.error("车辆能源类型不为空!");
		}
		String as = "E,B";
		int result1 = as.indexOf(vehicleEnergyType);
		if (result1 == -1) {
			return Result.error("车辆能源类型不正确! 车辆能源类型为天然气、柴油其他类型暂不支持");
		}

		return gasService.rechargeBack( id , money  , vehicleEnergyType);
	}




	@AutoLog(value = "油气回收")
	@ApiOperation(value = "油气回收", notes = "油气回收")
	@PutMapping(value = "/back")
	public Result<?> back(@RequestParam(name = "id", required = true) String id) {
		return gasService.back(id);
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "hy_shipping_note_gas-通过id查询", notes = "hy_shipping_note_gas-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		HyShippingNoteGas hyShippingNoteGas = hyShippingNoteGasService.getById(id);
		if (hyShippingNoteGas == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyShippingNoteGas);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param hyShippingNoteGas
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, HyShippingNoteGas hyShippingNoteGas) {
		return super.exportXls(request, hyShippingNoteGas, HyShippingNoteGas.class, "油气表");
	}


}
